package com.zzx.system.controller;

import com.alibaba.fastjson.JSONObject;
import com.zzx.common.constant.Constants;
import com.zzx.common.core.domain.entity.SysMenu;
import com.zzx.common.core.domain.entity.SysUser;
import com.zzx.common.core.domain.model.LoginBody;
import com.zzx.common.core.domain.model.LoginUser;
import com.zzx.common.core.text.Convert;
import com.zzx.entity.ApiResult;
import com.zzx.system.service.impl.SysPermissionService;
import com.zzx.utils.DateUtils;
import com.zzx.utils.StringUtils;
//import com.zzx.system.service.ISysConfigService;
import com.zzx.system.service.ISysMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * 登录验证
 *
 * @author ruoyi
 */
@RestController
@Api(tags = "系统登录管理")
public class SysLoginController {
//    @Autowired
//    private SysLoginService loginService;

    @Autowired
    private ISysMenuService menuService;

    @Autowired
    private SysPermissionService permissionService;

//    @Autowired
//    private TokenService tokenService;

//    @Autowired
//    private ISysConfigService configService;

    /**
     * 登录方法
     *
     * @param loginBody 登录信息
     * @return 结果
     */
//    @ApiOperation("用户登录")
//    @PostMapping("/login")
//    public ApiResult login(@RequestBody LoginBody loginBody) {
//        ApiResult ajax = ApiResult.success();
//        // 生成令牌
//        String token = loginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(),
//                loginBody.getUuid());
//        ajax.put(Constants.TOKEN, token);
//        return ajax;
//    }

    /**
     * 获取用户信息
     *
     * @return 用户信息
     */
    @ApiOperation("获取当前登录用户信息")
    @GetMapping("getInfo")
    public ApiResult getInfo() {
//        LoginUser loginUser = SecurityUtils.getLoginUser();
//        SysUser user = loginUser.getUser();
        SysUser user = null;
        // 角色集合
        Set<String> roles = permissionService.getRolePermission(user);
        // 权限集合
        Set<String> permissions = permissionService.getMenuPermission(user);
//        if (!loginUser.getPermissions().equals(permissions))
//        {
//            loginUser.setPermissions(permissions);
//            tokenService.refreshToken(loginUser);
//        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("user", user);
        jsonObject.put("roles", roles);
        jsonObject.put("permissions", permissions);
//        ajax.put("isDefaultModifyPwd", initPasswordIsModify(user.getPwdUpdateDate()));
//        ajax.put("isPasswordExpired", passwordIsExpiration(user.getPwdUpdateDate()));
        return ApiResult.ok(jsonObject);
    }

    /**
     * 获取路由信息
     *
     * @return 路由信息
     */
//    @ApiOperation("获取用户路由信息")
//    @GetMapping("getRouters")
//    public ApiResult getRouters() {
//        Long userId = SecurityUtils.getUserId();
//        List<SysMenu> menus = menuService.selectMenuTreeByUserId(userId);
//        return ApiResult.success(menuService.buildMenus(menus));
//    }

    // 检查初始密码是否提醒修改
//    public boolean initPasswordIsModify(Date pwdUpdateDate) {
//        Integer initPasswordModify = Convert.toInt(configService.selectConfigByKey("sys.account.initPasswordModify"));
//        return initPasswordModify != null && initPasswordModify == 1 && pwdUpdateDate == null;
//    }

    // 检查密码是否过期
//    public boolean passwordIsExpiration(Date pwdUpdateDate) {
//        Integer passwordValidateDays = Convert.toInt(configService.selectConfigByKey("sys.account.passwordValidateDays"));
//        if (passwordValidateDays != null && passwordValidateDays > 0) {
//            if (StringUtils.isNull(pwdUpdateDate)) {
//                // 如果从未修改过初始密码，直接提醒过期
//                return true;
//            }
//            Date nowDate = DateUtils.getNowDate();
//            return DateUtils.differentDaysByMillisecond(nowDate, pwdUpdateDate) > passwordValidateDays;
//        }
//        return false;
//    }
}
